from wordcloud import WordCloud  # 词云的生成
import matplotlib.pyplot as plt   # 画图之用
import matplotlib.image as mpimg  # mpimg 用于读取图片


def draw_word_cloud(text_path, img_path=None, background_color="white",
                    font_path='C:\\Windows\\Fonts\\simfang.ttf',
                    min_font_size=15, max_font_size=100, width=500, max_words=300, word_could_save_path=None):
    img = None
    if img_path is not None:
        img = mpimg.imread(img_path)
    wc = WordCloud(
        background_color=background_color,  # 背景颜色
        max_words=max_words,  # 显示最大词数
        font_path=font_path,  # 使用字体
        min_font_size=min_font_size,
        max_font_size=max_font_size,
        width=width,  # 图幅宽度
        mask=img
    )
    with open(text_path, 'r', encoding='utf8') as f:
        text = f.read()
    myword = wc.generate(text)
    if word_could_save_path is not None:
        wc.to_file(word_could_save_path)
    plt.imshow(myword)
    plt.axis("off")  # 去除坐标框架
    plt.show()


if __name__ == '__main__':
    draw_word_cloud(r'.\data\txt\wc_count1.txt', img_path=r'.\data\img\love.jpg')
